package com.lanchetech.merchant.controller;


import com.lanchetech.bean.request.*;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.vo.*;
import com.lanchetech.common.exception.ResultCodeException;
import com.lanchetech.entity.ShareActivity;
import com.lanchetech.entity.ShareActivitySpu;
import com.lanchetech.merchant.aop.OperatorLog;
import com.lanchetech.service.ShareActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(value = "分享赚管理接口", tags = "分享赚管理接口")
@RequestMapping("/api/merchant/sher")
@RestController
public class MerchantShareActivityController {

    @Autowired
    private ShareActivityService shareActivityService;

    @PostMapping("/getShareActivity")
    @PreAuthorize("hasAuthority('marketing.share.list')")
    @ApiOperation(value = "获取分享赚列表")
    public ResultData<BasePageResp<ShareActivityVo>> getShareActivity(@RequestBody ShareActivityReq shareActivityReq) {
        return shareActivityService.getShareActivity(shareActivityReq);
    }

    @PostMapping("/editShareActivity")
    @OperatorLog(value = "创建/编辑 分享赚活动")
    @PreAuthorize("hasAuthority(' marketing.share-profit.share-profit-list.edit.auth')")
    @ApiOperation(value = "创建/编辑 分享赚活动")
    public BaseResp editShareActivity(@RequestBody ShareActivity shareActivity) {
        BaseResp baseResp = new BaseResp();
        try {
            baseResp = shareActivityService.editSharActivity(shareActivity);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @DeleteMapping("/deleteShareById/{shareActivityId}")
    @OperatorLog(value = "删除分享赚")
    @PreAuthorize("hasAuthority('marketing.share-profit.share-profit-list.delete.auth')")
    @ApiOperation(value = "删除分享赚")
    public BaseResp deleteShareById(@PathVariable Long shareActivityId) {
        BaseResp baseResp = new BaseResp();
        try {
            baseResp = shareActivityService.deleteShareById(shareActivityId);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @PostMapping("/bindSpu")
    @OperatorLog(value = "绑定商品")
    @ApiOperation(value = "绑定商品")
    @PreAuthorize("hasAuthority('marketing.share-profit.share-profit-list.bind.auth')")
    public BaseResp bindSku(@RequestBody ShareActivitySpuReq req) {
        BaseResp baseResp = new BaseResp();
        try {
            List<String> exceptionList = new ArrayList<>();
            shareActivityService.bindSku(req.getShareActivitySpu(), req.getActivityId(), exceptionList);
            baseResp.setErrorCodes(exceptionList);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @GetMapping("/getBindSpu")
    @ApiOperation(value = "获取绑定商品列表")
    @PreAuthorize("hasAuthority('marketing.share.getspu')")
    public ResultData<List<SpuItemVO>> getBindSpu(@RequestParam Long shareActivityId) {
        return shareActivityService.getBindSpu(shareActivityId);
    }

    @PostMapping("/removeSpu/{shareActivitySpuId}")
    @OperatorLog(value = "解除绑定商品")
    @PreAuthorize("hasAuthority('marketing.share-profit.share-profit-list.remove.auth')")
    @ApiOperation(value = "解除绑定商品")
    public BaseResp removeSpu(@PathVariable Long shareActivitySpuId) {
        BaseResp baseResp = new BaseResp();
        try {
            shareActivityService.removeSpu(shareActivitySpuId);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @PostMapping("/whether/{id}")
    @OperatorLog(value = "开启/关闭")
    @PreAuthorize("hasAuthority('marketing.share-profit.share-profit-list.whether.auth')")
    @ApiOperation(value = "开启/关闭")
    public BaseResp whether(@PathVariable Long id) {
        BaseResp baseResp = new BaseResp();
        try {
            shareActivityService.whether(id);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @GetMapping("/getShareDetailed")
    @OperatorLog(value = "佣金/嗨币列表")
    @PreAuthorize("hasAuthority('marketing.share.detailed')")
    @ApiOperation(value = "佣金/嗨币列表")
    public ResultData<BasePageResp<ShareDetailedVo>> getShareDetailed(@RequestBody ShareDetailedReq req) {
        ResultData<BasePageResp<ShareDetailedVo>> vo = shareActivityService.getShareDetailed(req);
        return vo;
    }
}
